﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CuaHangLaptop.BIZ;

namespace QuanLy.GUI
{
    public partial class CTPhieuNhap_GUI : Form
    {
        List<CTPhieuNhap_BIZ> list_ctpn = new List<CTPhieuNhap_BIZ>();
        List<Laptop_BIZ> list_lap = new List<Laptop_BIZ>();
        CTPhieuNhap_BIZ current_ctpn = new CTPhieuNhap_BIZ();
        bool updated = true;
        string MaPN;
        string MaNSX;

        public CTPhieuNhap_GUI(string _MaPN, string _MaNSX)
        {
            InitializeComponent();
            lb_nsx.Text += " " + _MaNSX;
            lb_pn.Text += " " + _MaPN;
            MaPN = _MaPN;
            MaNSX = _MaNSX;
            GridView_CTPN.AutoGenerateColumns = false;
            Frm_Load();
        }

        void Frm_Load()
        {
            list_ctpn = current_ctpn.GetAll_CTPN_ByPN(MaPN);
            list_lap = current_ctpn.Laptop.GetAll_Laptop_ByNSX(MaNSX);
            GridView_CTPN.DataSource = null;
            GridView_CTPN.DataSource = list_ctpn;
            cb_laptop.DataSource = list_lap;
            cb_laptop.ValueMember = "MaLaptop";
            cb_laptop.DisplayMember = "TenLaptop";
            Disable_All(true);
        }

        private void btn_Them_Click(object sender, EventArgs e)
        {
            Reset();
            Disable_All(false);
            updated = false;
        }

        private void btn_Luu_Click(object sender, EventArgs e)
        {
            try
            {        
                if (!updated)
                {
                    current_ctpn = new CTPhieuNhap_BIZ(-1, txt_MaCTPN.Text, MaPN, cb_laptop.SelectedValue.ToString(), int.Parse(txt_Soluong.Text));
                    list_ctpn.Add(current_ctpn);
                }
                else  Get_Val();
                
                current_ctpn.Save();
                GridView_CTPN.DataSource = null;
                GridView_CTPN.DataSource = list_ctpn;
                Disable_All(true);
                updated = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi!\n" + ex.Message);
            }
        }

        private void btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void GridView_CTPN_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (GridView_CTPN.SelectedRows.Count > 0)
            {
                if (GridView_CTPN.Columns[e.ColumnIndex].HeaderText == "Xóa")
                {
                    current_ctpn = (CTPhieuNhap_BIZ)GridView_CTPN.Rows[e.RowIndex].DataBoundItem;
                    list_ctpn.Remove(current_ctpn);
                    current_ctpn.Delete();
                    GridView_CTPN.DataSource = null;
                    GridView_CTPN.DataSource = list_ctpn;
                }
                if (GridView_CTPN.Columns[e.ColumnIndex].HeaderText == "Sửa")
                {
                    current_ctpn = (CTPhieuNhap_BIZ)GridView_CTPN.Rows[e.RowIndex].DataBoundItem;
                    Disable_All(false);
                }
            }
        }

        private void GridView_CTPN_SelectionChanged(object sender, EventArgs e)
        {
            if (GridView_CTPN.SelectedRows.Count > 0)
            {
                current_ctpn = (CTPhieuNhap_BIZ)GridView_CTPN.SelectedRows[0].DataBoundItem;
                Set_Val();
                Disable_All(true);
            }
        }

        void Reset()
        {
            int id = current_ctpn.Get_Lasted_ID();
            txt_MaCTPN.Text = "CTPN" + (id + 1);
            txt_Soluong.Text = "0";
            cb_laptop.SelectedIndex = 0;
        }

        void Get_Val()
        {
            current_ctpn.Laptop = current_ctpn.Laptop.Get_Laptop_ByCode(cb_laptop.SelectedValue.ToString());
            current_ctpn.MaCTphieunhap = txt_MaCTPN.Text;
            current_ctpn.Soluong = int.Parse(txt_Soluong.Text);
        }

        void Set_Val()
        {
            cb_laptop.SelectedValue = current_ctpn.Laptop.MaLaptop;
            txt_Soluong.Text = current_ctpn.Soluong.ToString();
            txt_MaCTPN.Text = current_ctpn.MaCTphieunhap;
        }

        void Disable_All(bool _disable)
        {
            txt_MaCTPN.ReadOnly = txt_Soluong.ReadOnly = _disable;
            cb_laptop.Enabled = !_disable;
        }

        private void txt_Soluong_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }
        }
    }
}
